package ming.model.type;


import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@Entity
@Table(name = "t_limitconfig")
@DynamicInsert(true)
@DynamicUpdate(true)
public class LimitConfig {

    private String pid;// 虚拟属性，用于获得当前机构的父机构ID

    private String id;
    private Date createdatetime;
    private Date updatedatetime;
    private String name;
    private String code;
    private String iconCls;
    private Integer seq;
    private Integer maxnum;
    private Integer currentnum;
    private Integer limitdays;
    private String currentseq;
    private String managearea;
    private String seqno;
    private String orgid;
    private String isqueue;  //0-直接可以办理， 1-需要排队办理
    private LimitConfig area;
    private Set<LimitConfig> areas = new HashSet<LimitConfig>(0);
    private Set<Terminal> terminals = new HashSet<Terminal>(0);

    @Id
    @Column(name = "ID", unique = true, nullable = false, length = 36)
    public String getId() {
        if (!StringUtils.isBlank(this.id)) {
            return this.id;
        }
        return UUID.randomUUID().toString();
    }

    public void setId(String id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "AREA_ID")
    public LimitConfig getArea() {
        return this.area;
    }

    public void setArea(LimitConfig area) {
        this.area = area;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "UPDATEDATETIME", length = 7)
    public Date getUpdatedatetime() {
        if (this.updatedatetime != null)
            return this.updatedatetime;
        return new Date();
    }

    public void setUpdatedatetime(Date updatedatetime) {
        this.updatedatetime = updatedatetime;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATEDATETIME", length = 7)
    public Date getCreatedatetime() {
        if (this.createdatetime != null)
            return this.createdatetime;
        return new Date();
    }

    public void setCreatedatetime(Date createdatetime) {
        this.createdatetime = createdatetime;
    }

    @Column(name = "NAME", length = 200)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "CODE", length = 200)
    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(name = "ICONCLS", length = 100)
    public String getIconCls() {
        return this.iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    @Column(name = "SEQ", precision = 8, scale = 0)
    public Integer getSeq() {
        return this.seq;
    }

    public void setSeq(Integer seq) {
        this.seq = seq;
    }

    @Column(name = "MAXNUM", precision = 8, scale = 0)
    public Integer getMaxnum() {
        return maxnum;
    }

    public void setMaxnum(Integer maxnum) {
        this.maxnum = maxnum;
    }

    @Column(name = "CURRENTNUM", precision = 8, scale = 0)
    public Integer getCurrentnum() {
        return currentnum;
    }

    public void setCurrentnum(Integer currentnum) {
        this.currentnum = currentnum;
    }

    @Column(name = "LIMITDAYS", precision = 8, scale = 0)
    public Integer getLimitdays() {
        return limitdays;
    }

    public void setLimitdays(Integer limitdays) {
        this.limitdays = limitdays;
    }

    @Column(name = "CURRENTSEQ", length = 200)
    public String getCurrentseq() {
        return currentseq;
    }

    public void setCurrentseq(String currentseq) {
        this.currentseq = currentseq;
    }

    @Column(name = "MANAGEAREA", length = 4000)
    public String getManagearea() {
        return managearea;
    }

    public void setManagearea(String managearea) {
        this.managearea = managearea;
    }

    @Column(name = "SEQNO", length = 50)
    public String getSeqno() {
        return seqno;
    }

    public void setSeqno(String seqno) {
        this.seqno = seqno;
    }

    @Column(name = "ORGID", length = 36)
    public String getOrgid() {
        return orgid;
    }

    public void setOrgid(String orgid) {
        this.orgid = orgid;
    }

    @Column(name = "ISQUEUE", length = 1)
    public String getIsqueue() {
        return isqueue;
    }

    public void setIsqueue(String isqueue) {
        this.isqueue = isqueue;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "area", cascade = CascadeType.ALL)
    public Set<LimitConfig> getAreas() {
        return this.areas;
    }

    public void setAreas(Set<LimitConfig> areas) {
        this.areas = areas;
    }


    @OneToMany(fetch = FetchType.LAZY, mappedBy = "area")
    public Set<Terminal> getTerminals() {
        return terminals;
    }

    public void setTerminals(Set<Terminal> terminals) {
        this.terminals = terminals;
    }

    /**
     * 用于业务逻辑的字段，注解@Transient代表不需要持久化到数据库中
     *
     * @return
     */
    @Transient
    public String getPid() {
        if (area != null && !StringUtils.isBlank(area.getId())) {
            return area.getId();
        }
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }
}
